package com.vote.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Class Name: ResponseUtils<br>
 * Description: HttpServletResponse帮助类<br>
 * Sample: 该类的典型使用方法和用例<br>
 * Author: gzq<br>
 * Date: 2012-10-26<br>
 * --------------------------------------------------<br>
 * 修改人　　　　修改日期　　　　　修改描述<br>
 * gzq　　　2012-10-26　　　　　　创建<br>
 * --------------------------------------------------<br>
 * @Version  Ver1.0<br>
 */
public final class ResponseUtils {
	public static final Logger log = LoggerFactory
			.getLogger(ResponseUtils.class);

	/**
	 * 发送文本。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderText(String text,HttpServletResponse response) {
		render("text/plain;charset=UTF-8", text,response);
	}

	/**
	 * 发送json。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderJson(String text,HttpServletResponse response) {
		render("application/json;charset=UTF-8", text,response);
	}

	/**
	 * 发送xml。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderXml(String text,HttpServletResponse response) {
		render("text/xml;charset=UTF-8", text,response);
	}

	/**
	 * 发送内容。使用UTF-8编码。
	 * 
	 * @param response
	 * @param contentType
	 * @param text
	 */
	public static void render(String contentType,
			String text,HttpServletResponse response) {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}
}
